package leetcode.leetcode_238;

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] pre = new int[n];
        int[] after = new int[n];
        pre[0] = nums[0];
        after[n-1] = nums[n-1];
        for (int i = 1; i < n; i++){
            pre[i] = pre[i-1]*nums[i];
            after[n-i-1] = after[n-i]*nums[n-i-1];
        }
        int[] ans = new int[n];
        for (int i = 0; i < n; i++){
            if(i==0){
                ans[i]=after[i+1];
            }
            else if (i==n-1){
                ans[i]=pre[i-1];
            }
            else{
                ans[i] = pre[i-1]*after[i+1];
            }
        }
        return ans;

    }
}